﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class cms_AddNewDeployment : System.Web.UI.Page
{
    private DBClass db = new DBClass();
    private int DepId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["DepId"] != null)
        {
            Int32.TryParse(Request.Params["DepId"].ToString(), out DepId);
        }
        if (!IsPostBack)
        {
            LoadData();
        }        
    }

    private void LoadData()
    {
        ddlType.DataSource = db.GetAllType();
        ddlType.DataBind();
        LoadInfoDevelopment();
    }

    private void LoadInfoDevelopment()
    {
        if (DepId > 0)
        {
            DataTable dt = db.GetInfoDeploymentById(DepId);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                txtDeployment.Text = row["Deployment"].ToString();
                ddlType.SelectedValue = row["TypeId"].ToString();
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int TypeId = 0;
        Int32.TryParse(ddlType.SelectedValue, out TypeId);
        if (TypeId > 0)
        {
            if (DepId > 0)
            {
                db.UpdateDeployment(txtDeployment.Text, TypeId, DepId);
            }
            else
            {
                db.InsertDeployment(txtDeployment.Text, TypeId);
            }

            Response.Redirect("Deployment.aspx");
        }
    }
}